package com.serkol.ams.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.Pictures;
import com.serkol.ams.mapper.PicturesMapper;
import com.serkol.ams.service.PicturesService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author pingan
 * @description 针对表【pictures】的数据库操作Service实现
 * @createDate 2022-09-16 19:40:17
 */
@Service
public class PicturesServiceImpl extends ServiceImpl<PicturesMapper, Pictures>
        implements PicturesService {

    @Value("${upload.path}")
    private String uploadpath;
    @Value("${upload.host}")
    private String uploadhost;
    @Autowired
    private WxMaService wxMaService;
    @Override
    public MyPage<Pictures> findByList(int pageSize, int page1, int type) {
        QueryWrapper<Pictures> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Pictures::getIstype,type);
        queryWrapper.orderByDesc("ID");
        Page<Pictures> page = new Page<>(page1, pageSize);
        IPage<Pictures> loginLogPage = this.baseMapper.selectPage(page, queryWrapper);
        return MyPage.parsePage(loginLogPage);
    }

    @Cacheable(cacheNames = "qrcode", key = "#key")
    @Override
    public File invalidQrCode(String key, String pages) throws WxErrorException {

     //   String   key = Base62.encode(pages);
   
            File file = wxMaService.getQrcodeService().createWxaCode(pages);




        return file;
    }


     @Cacheable(cacheNames = "qrcode", key = "#key")
    @Override
    public String invalidQrCodeUrl(String key, String pages) throws WxErrorException, IOException {
        // 构建本地文件路径
        String fileName ="poster/qrcode/" + key + ".png";
        String localFilePath = uploadpath + fileName;

        // 创建文件对象
        File file = new File(localFilePath);

        // 确保父目录存在
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        // 获取二维码文件内容
        File qrcodeFile = wxMaService.getQrcodeService().createWxaCode(pages);

        // 使用 try-with-resources 确保资源关闭
        try (FileOutputStream fos = new FileOutputStream(file)) {
            // 将文件内容写入到本地文件
            fos.write(java.nio.file.Files.readAllBytes(qrcodeFile.toPath()));
        }

        // 返回完整的访问路径
        return uploadhost + fileName;


    }
}




